package com.loong.life.handler;

import com.loong.common.domain.Pager;
import com.loong.common.enumerate.ProductStatus;
import com.loong.common.exception.GlobalException;
import com.loong.common.executor.BatchExecutor;
import com.loong.common.utils.TransformUtils;
import com.loong.life.business.AgentProductBusiness;
import com.loong.life.common.backend.param.AgentProductParam;
import com.loong.life.common.backend.param.AgentProductQueryParam;
import com.loong.life.common.backend.result.AgentProductResult;
import com.loong.life.common.forehead.result.AgentProductForeheadResult;
import com.loong.life.model.AgentProduct;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @Description: 处理有关代理商产品业务的handler
 * @Author: 刘群
 * @Date: 2020/11/26 16:50
 */
@Service
public class AgentProductHandler {

    @Resource
    private AgentProductBusiness productBusiness;

    @Resource
    private BatchExecutor batchExecutor;

    /**
     * 后台新增代理商产品
     * @param param 产品参数
     */
    public void saveAgentProduct(AgentProductParam param){
        param.setCreateTime(new Date());
        //新增的产品都是未开售的
        param.setStatus(ProductStatus.UN_SALE.getCode());
        productBusiness.save(TransformUtils.transform(param, AgentProduct.class));
    }

    /**
     * 后台更新代理商产品
     * @param param 产品参数
     */
    public void updateAgentProduct(AgentProductParam param){
        param.setLastUpdateTime(new Date());
        productBusiness.updateByPrimaryKeySelective(TransformUtils.transform(param, AgentProduct.class));
    }

    /**
     * 后台批量删除代理商产品
     * @param idList 产品id集合
     */
    public void batchDeleteAgentProduct(List<Long> idList){
        batchExecutor.batchExecute(idList,id -> id.toString(),(item) -> {
            //根据id查询产品，若产品正在售卖中则不能删除
            AgentProduct product = productBusiness.findById(item);
            if (ObjectUtils.isNotEmpty(product.getStatus())&&ProductStatus.ON_SELL.getCode() == product.getStatus()){
                throw new GlobalException("agent product is used");
            }else {
                productBusiness.deleteById(item);
            }
        });
    }

    /**
     * 后台分页查询代理商产品
     * @param param 查询参数
     * @return 分页结果
     */
    public Pager<AgentProductResult> pageQueryAgentProduct(AgentProductQueryParam param){
        Pager<AgentProduct> pager = productBusiness.pageQuery(param, () -> productBusiness.findAgentProductByParam(param));
        return TransformUtils.transformPage(pager,AgentProductResult.class);
    }

    /**
     * 前台根据代理商id查询代理商产品
     * @param agentId 代理商id
     * @return 代理商产品集合
     */
    public List<AgentProductForeheadResult> findAgentProductByAgentId(Long agentId){
        AgentProductQueryParam param = AgentProductQueryParam.builder()
                .agentId(agentId)
                .status(ProductStatus.ON_SELL.getCode())
                .build();
        return TransformUtils.transformList(productBusiness.findAgentProductByParam(param),AgentProductForeheadResult.class);
    }

    /**
     * 前台根据代理商产品id查询代理商产品
     * @param id 代理商产品id
     * @return 代理商产品
     */
    public AgentProductForeheadResult findAgentProductById(Long id){
        AgentProduct agentProduct = productBusiness.findById(id);
        if (ObjectUtils.isEmpty(agentProduct)) return null;
        return TransformUtils.transform(agentProduct,AgentProductForeheadResult.class);
    }
}
